package io.zhiller.ollamaclient

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Modifier
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.navigation.compose.rememberNavController
import io.zhiller.ollamaclient.api.git.GitService
import io.zhiller.ollamaclient.di.viewmodel.AppVM
import io.zhiller.ollamaclient.ui.navigation.NavGraphs
import io.zhiller.ollamaclient.ui.theme.OllamaClientTheme
import io.zhiller.ollamaclient.utils.LoggerUtils
import org.koin.androidx.compose.koinViewModel
import org.koin.compose.koinInject

class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    enableEdgeToEdge()
    setContent {
      // 创建 LifecycleOwner
      AppLifecycleObserver()
      
      OllamaClientTheme {
        val navController = rememberNavController()
        Surface(modifier = Modifier.fillMaxSize()) {
          NavGraphs(navController = navController)
        }
      }
    }
  }
}

@Composable
fun AppLifecycleObserver() {
  // 获取当前 Compose 组件的 LifecycleOwner
  val lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current
  
  val gitService = koinInject<GitService>()
  val appVm = koinViewModel<AppVM>()
  
  // 使用 DisposableEffect 监听生命周期
  DisposableEffect(lifecycleOwner) {
    // 创建 LifecycleEventObserver
    val observer = LifecycleEventObserver { _, event ->
      when (event) {
        Lifecycle.Event.ON_CREATE -> {
          LoggerUtils.debug("Lifecycle: ON_CREATE")
          gitService.getApiUrl(
            onSuccess = {
              LoggerUtils.debug("GitService: ON_SUCCESS")
              LoggerUtils.info("GitService: ON_SUCCESS: ${it.data}")
              appVm.updateApiUrl(it.data?.content ?: "")
            },
            onError = {
              LoggerUtils.error(it.toString())
            }
          )
        }
        
        Lifecycle.Event.ON_START -> {
          LoggerUtils.debug("Lifecycle: ON_START")
        }
        
        Lifecycle.Event.ON_RESUME -> {
          LoggerUtils.debug("Lifecycle: ON_RESUME")
        }
        
        Lifecycle.Event.ON_PAUSE -> {
          LoggerUtils.debug("Lifecycle: ON_PAUSE")
        }
        
        Lifecycle.Event.ON_STOP -> {
          LoggerUtils.debug("Lifecycle: ON_STOP")
        }
        
        Lifecycle.Event.ON_DESTROY -> {
          LoggerUtils.debug("Lifecycle: ON_DESTROY")
        }
        
        Lifecycle.Event.ON_ANY -> {
          LoggerUtils.debug("Lifecycle: ON_ANY")
        }
      }
    }
    
    // 注册观察者
    lifecycleOwner.lifecycle.addObserver(observer)
    
    // 在组件销毁时注销观察者
    onDispose {
      lifecycleOwner.lifecycle.removeObserver(observer)
    }
  }
}